package com.itbaima.csmall.stock.webapi.service.impl;

import com.itbaima.csmall.commons.exception.CoolSharkServiceException;
import com.itbaima.csmall.commons.pojo.stock.dto.StockReduceCountDTO;
import com.itbaima.csmall.commons.restful.ResponseCode;
import com.itbaima.csmall.stock.service.StockService;
import com.itbaima.csmall.stock.webapi.mapper.StockMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>库存业务实现类</p>
 *
 * @author Chen
 * @since 2023/2/20 14:47:47
 */
@Slf4j
@Service
@DubboService
public class StockServiceImpl implements StockService {

    @Autowired
    private StockMapper stockMapper;

    @Override
    public void reduceCommodityCount(StockReduceCountDTO stockReduceCountDTO) {
        int rows = stockMapper.updateStockCountByCommodityCode(stockReduceCountDTO.getCommodityCode(), stockReduceCountDTO.getReduceCount());
        if (rows == 0) {
            throw new CoolSharkServiceException(ResponseCode.BAD_REQUEST, "库存不足");
        }
        log.info("商品库存减少完成");
    }

}
